﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;

namespace Utilities.Helper
{
    
    public static class CommonExtensionMethods
    {

        static void CatchExceptions(Exception e)
        { 
        
        }

        /// <summary>
        /// To check whether the given object exists in the string array or not
        /// </summary>
        /// <param name="str"></param>
        /// <param name="array"></param>
        /// <returns></returns>
        public static bool IsInArray(this object obj, params string[] array)
        {
            bool retvalue = false;

            string value = "";
            if (obj.IsNotNullAndEmpty())
            {
                value = obj.ToString();
                retvalue = value.IsInArray(array);
            }

            return retvalue;
        }

        /// <summary>
        /// To check whether the given string exists in the array or not
        /// </summary>
        /// <param name="str"></param>
        /// <param name="array"></param>
        /// <returns></returns>
        public static bool IsInArray(this string str, params string[] array)
        {
            bool status = false;
            if (array != null && array.Length > 0)
            {
                for (int i = 0; i < array.Length; i++)
                {
                    if (!string.IsNullOrEmpty(str) && !string.IsNullOrEmpty(array[i]) && str.ToLower() == array[i].ToLower())
                    {
                        status = true; break;
                    }
                }
            }
            return status;
        }

         
        /// <summary>
        /// Converts an object value into string value
        /// </summary>
        /// <param name="value"></param>
        /// <returns>return parsed value otherwise empty string</returns>
        public static string ToStringFromObject(this object value)
        {
            string _value = "";
            if (value.IsNotNullAndEmpty()) _value = value.ToString();
            return _value;
        }

        /// <summary>
        /// To check whether the given object is not null and not empty
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsNotNullAndEmpty(this object value)
        {
            bool _Status = false;
                if (value != null && !string.IsNullOrEmpty(value.ToString()))
                    _Status = true;
            return _Status;
        }

        /// <summary>
        /// To check whether the given DataView is not null and having more than one row
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsNotNullAndHasRows(this DataView value)
        {
            bool _Status = false;
                if (value != null && value.Count > 0)
                    _Status = true;
            return _Status;
        }

        /// <summary>
        /// To convert an object value to int
        /// </summary>
        /// <param name="value"></param>
        /// <returns>0 - if any error occurs</returns>
        public static int ToInt(this object value)
        {
            int _value = 0;
                if (value.IsNotNullAndEmpty())
                    int.TryParse(value.ToString(), out _value);
            return _value;
        }

        /// <summary>
        /// ThisStringHasNoSpacesButItDoesHaveCapitals
        /// --- function returns : This String Has No Spaces But It Does Have Capitals
        /// </summary>
        /// <param name="value"></param>
        /// <returns>This String Has No Spaces But It Does Have Capitals</returns>
        public static string AddSpaceBeforeCAPS(this string value)
        {
            if (string.IsNullOrEmpty(value) == false)
                return System.Text.RegularExpressions.Regex.Replace(value, "[A-Z]", " $0").Trim();
            else return value;
        }

        /// <summary>
        /// Converts the given object to DateTime object, returns DateTime.MinValue if there is any error occurs
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static DateTime ToDateTime(this object value)
        {
            DateTime _value = new DateTime();
            _value = DateTime.MinValue;
                if (value.IsNotNullAndEmpty()) DateTime.TryParse(value.ToString(), out _value);
            return _value;
        }

        /// <summary>
        /// To remove the html tags from the given string
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string RemoveHTMLTags(this object value)
        {
            return (value.IsNotNullAndEmpty() ? System.Text.RegularExpressions.Regex.Replace(value.ToString(), @"<(.|\n)*?>", string.Empty) : "");
        }

        /// <summary>
        /// To convert the given object to bool, default - returns false;
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool ToBool(this object value)
        {
            bool _value = false;
                if (bool.TryParse(value.ToStringFromObject(), out _value))
                {

                }
                else if (value.IsNotNullAndEmpty())
                {
                    if (value.ToString() == "1") _value = true;
                }
            return _value;
        }

        public static string[] ToArray(this DataView dView, string ColumnName)
        {
            string sVal = string.Empty;

            if (dView != null && dView.Count > 0)
            {
                for (int i = 0; i < dView.Count; i++)
                {
                    sVal += dView[i][ColumnName].ToString() + "|";
                }
            }
            return sVal.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
        }

        //public static string ToSQLString(this string value){
        //    return Utilities.SQLValueConversion(value,Declarations.DBDataType.STRING);
        //}

        //public static string ToSQLDateTime(this DateTime value)
        //{
        //    try
        //    {
        //        return Utilities.SQLValueConversion(value.ToString(), Declarations.DBDataType.DATETIME);
        //    }
        //    catch (Exception)
        //    {
        //        return "GETDATE()";
        //    }
            
        //}

        //public static string ToSQLDateTime(this string value)
        //{
        //    return Utilities.SQLValueConversion(value, Declarations.DBDataType.DATETIME);
        //}

        //public static string ToSQLBool(this bool value)
        //{
        //    return Utilities.SQLValueConversion(value.ToString(), Declarations.DBDataType.BOOL);
        //}

        //public static string ToSQLBool(this bool? value)
        //{
        //    return Utilities.SQLValueConversion(value.ToString(), Declarations.DBDataType.BOOL);
        //}

        //public static string Encrypt(this string value) {
        //    string passPhrase = "Pas5pr@se";        // can be any string
        //    string saltValue = "s@1tValue";        // can be any string
        //    string hashAlgorithm = "SHA1";             // can be "MD5"
        //    int passwordIterations = 2;                  // can be any number
        //    string initVector = "@1B2c3D4e5F6g7H8"; // must be 16 bytes
        //    int keySize = 256;                // can be 192 or 128

        //    return Cryptography.Encrypt(value, passPhrase, saltValue, hashAlgorithm, passwordIterations, initVector, keySize);
        //}

        //public static string Decrypt(this string value)
        //{
        //    string passPhrase = "Pas5pr@se";        // can be any string
        //    string saltValue = "s@1tValue";        // can be any string
        //    string hashAlgorithm = "SHA1";             // can be "MD5"
        //    int passwordIterations = 2;                  // can be any number
        //    string initVector = "@1B2c3D4e5F6g7H8"; // must be 16 bytes
        //    int keySize = 256;                // can be 192 or 128
        //    string decrypted = value;

        //    try
        //    {
        //        decrypted = Cryptography.Decrypt(value, passPhrase, saltValue, hashAlgorithm, passwordIterations, initVector, keySize);
        //    }catch(Exception ex){
            
        //    }

        //    if (value.IsNotNullAndEmpty() && !decrypted.IsNotNullAndEmpty())
        //        decrypted = value;

        //    return decrypted;
        //}

        public static void AddtoLogs(string msg)
        {
            Logger.Log(msg);
            
        }
    }

    
    
    

    public class Declarations
    {
        public static string DateFormat = "dd-MMM-yyyy";
        public static string DateTimeFormat = "dd-MMM-yyyy hh:mm";
        public static string TimeFormat = "hh:mm";

        public enum DBDataType
        {
            STRING = 0, NUMERIC = 1, BOOL = 3, DATETIME = 4
        }

        public enum DBRetType
        {
            NONE = 0, DATAVIEW = 1, XML = 2, OBJECT = 3, DATASET = 4, DATATABLE = 5, SCALAR = 6, OBJECTLIST = 7
        }
            
    }
}
